<?php
/**
* 文件操作类（含上传操作）
*
* 本程序主要作用管理文件和上传文件以及删除文件等
* 
* @category   Lib

* @copyright  Copyright (c) ink工作室 (http://www.inkphp.com)
* @author     张旭刚
* @version    v1.0 beta
*/
class File{
    private $thumbWidth;
    private $thumbHeight;
    private $limitSize; //kb
    private $limitType;
    private $limitWidth; //px
    private $limitHeight; //px
    private $isRename;
    private $uploadPath;
    /**
     * 构造方法 
     */
    public function __construct() {
        $this->setLimitHeight();
        $this->setLimitWidth();
        $this->setLimitSize();
        $this->setLimitType();
        $this->setThumbHeight();
        $this->setThumbWidth();
        $this->setIsRename();
        $this->setUploadPath();
    }
    
    /**
     * 设置上传的文件是否重新命名 true：是，false：不是，默认为重新命名
     * @param type $status 
     */
    public function setIsRename($status = true){
        $this->isRename = $status;
    }
    
    /**
     * 设置缩略图的宽度
     * @param type $width 
     */
    public function setThumbWidth($width = 300){
        $this->thumbWidth = $width;
    }
    /**
     * 设置缩略图的高度
     * @param type $height 
     */
    public function setThumbHeight($height = 200){
        $this->thumbHeight = $height;
    }
    /**
     * 设置最大允许上传的大小 单位：KB
     * @param type $size 
     */
    public function setLimitSize($size = 200){
        $this->limitSize = $size;
    }
    
    /**
     * 设置允许上传的文件类型
     * @param type $types 
     */
    public function setLimitType($types = array('gif','jpg','png','jpeg')){
        $this->limitType = $types;
    }
    
    /**
     * 设置允许上传的图片宽度
     * @param type $width 
     */
    public function setLimitWidth($width = 0){
        $this->limitWidth = $width;
    }
    
    /**
     * 设置允许上传的图片高度
     * @param type $height 
     */
    public function setLimitHeight($height = 0){
        $this->limitHeight = height;
    }
    
    /**
     * 设置图片上传到得服务器路径
     * @param type $path 
     */
    public function setUploadPath($path = './Data'){
        $this->uploadPath = $path;
    }
    
    /*取得当前目录下的文件和文件夹*/
    /**
     * 取得目录下的文件和目录列表
     * @param type $dir
     * @return type 
     */
    public function getDir($dir){
        $handler = opendir($dir);
        while($file = readdir($handler)){
            if($file != '.' && $file != '..'){
                $data[] = $file;
            }
        }
        return $data;
    }
    
    /**
     * 读取文件内容
     * @param type $file
     * @return type 
     */
    public function readFile($file){
        return file_get_contents($file);
    }
    
    /**
     * 写入文件内容
     * @param type $file
     * @param type $content
     * @return type 
     */
    public function writeFile($file,$content){
        return file_put_contents($file, $content);
    }
    
    /**
     * 上传文件
     * @param type $file
     * @return string 
     */
    public function uploadFile($file){
        if(empty($file)){
            return 'Error:file_information_is_empty';
        }
        $fileName = $file['name'];
        $source = $file['tmp_name'];
        $fileSize = $file['size'];
        if($this->checkSize($fileSize)){
            return 'Error:file_is_too_large';
        }elseif($this->checkType($fileName)){
            return 'Error:file_type_is_not_allow';
        }else{
            if(!file_exists($this->uploadPath)){
                $this->createDir($this->uploadPath);
            }
            $dest = $this->uploadPath.'/'.$this->getFileName($fileName);
            if(@copy($source, $dest)){
                $this->thumbFile($dest);
                return $dest;
            }else{
                return 'Error:file_upload_fail';
            }
        }
    }
    
    /**
     * 上传所有文件
     * @param type $data
     * @return string 
     */
    public function uploadAll($data){
        if(!empty($data)){
            foreach($data as $k => $v){
                $d[] = $this->uploadFile ($v);
            }   
        }else{
            return 'no_files';
        }
    }
    
    /**
     * 生成图片文件的缩略图
     * @param type $file 
     */
    private function thumbFile($file){
        
    }
    
    /**
     * 取得文件的扩展名
     * @param type $filename
     * @return type 
     */
    public function getExtName($filename){
        return strtolower(substr($filename, strrpos('.',$filename)+1, strlen($filename)));
    }
    
    /**
     * 判断文件或目录是否存在
     * @param type $path
     * @return type 
     */
    public function isExist($path){
        return file_exists($path);
    }
    
    /**
     * 创建多层目录
     * @param type $path 
     */
    public function createDir($path){
        $path = explode('/', $path);
        foreach($path as $key => $vlaue){
            $p .= $value.'/';
            if(!$this->isExist($p)){
                mkdir($p);
            }
        }
    }
    
    /**
     * 检查文件大小是否在允许范围之内
     * @param type $size
     * @return boolean 
     */
    private function checkSize($size){
        $size = round($size/1024,2);
        if($size > $this->limitSize){
            return true;
        }
        return false;
    }
    
    /**
     * 检查文件类型是否允许上传
     * @param type $fileName
     * @return boolean 
     */
    private function checkType($fileName){
        $extName = $this->getExtName($fileName);
        if(in_array($extName, $this->limitType)){
            return true;
        }
        return false;
    }
    
    /**
     * 检查文件宽度是否允许上传
     * @param type $width
     * @return boolean 
     */
    private function checkWidth($width){
        if($this->limitWidth < $width){
            return false;
        }
        return true;
    }
    
    /**
     * 检查文件高度是否允许上传
     * @param type $height
     * @return boolean 
     */
    private function checkHeight($height){
        if($this->limitHeight < $height){
            return false;
        }
        return true;
    }
    
    /**
     * 取得新的文件名
     * @param type $fileName
     * @param type $pre
     * @return string 
     */
    private function getFileName($fileName, $pre = null){
        if($this->isRename){
            $extName = $this->getExtName($fileName);
            $fName = uniqid($pre).'.'.$extName;
            return $fName;
        }
        return basename($fileName);
    }
    
    
}